home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 9 / The PC-SIG Library on CD ROM - Ninth Edition.iso / 1501_600 / DISK1533 / DISK1533.ZIP / WILD.C < prev   
Text File  |  1989-02-22  |  2KB  |  79 lines

  1. /*
  2.  * wild.c - wildcard expander
  3.  *
  4.  * S. Leoce 26 Jun 88    psj@va
  5.  *
  6.  * Purpose:
  7.  *    expands wildcards in passed arg, returning the next matching name
  8.  *     or null when no more files match.
  9.  *
  10.  * Usage:
  11.  *    initial call with the search pattern according to regular DOS
  12.  *    rules. '*' (none or more of any character), '?' (exactly one char).
  13.  *    subsequent calls with NULL argument to track through the directory,
  14.  *    subroutine expects that a non-NULL argument restarts the search
  15.  *    for a new pattern.
  16.  *
  17.  * Errors:
  18.  *    None.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <dir.h>
  23.  
  24. static    char * _Subroutine = "_wild - wildcard expander";
  25.  
  26. char *strcpy();
  27. char *strrev();
  28. char *strcat();
  29. char *strupr();
  30. char *strtok();
  31.  
  32. int findfirst();
  33. int findnext();
  34. int fnsplit();
  35.  
  36. void fnmerge();
  37.  
  38. int _FAttr ;        /* result from FNSPLIT */
  39.  
  40. char * _wild( arg )
  41. char * arg;
  42. {
  43.  
  44.     static char ext [MAXEXT];    /* file extension    */
  45.     static char dir [MAXDIR];    /* file directory    */
  46.     static char file [MAXFILE];    /* primary file name    */
  47.     static char drive [MAXDRIVE];    /* file drive specifier    */
  48.  
  49.     static struct ffblk ffblk;    /* file control block    */
  50.     static char name [MAXPATH];    /* full file path    */
  51.  
  52.     auto int done;            /* status from find    */
  53.  
  54.     if( arg ) {
  55.         done = findfirst( arg, &ffblk, 0 );
  56.         _FAttr = fnsplit( arg, drive, dir, file, ext );
  57.     }
  58.     else
  59.         done = findnext( &ffblk );
  60.  
  61.     if( done )
  62.         return (NULL);
  63.  
  64.     if( _FAttr & WILDCARDS ) {
  65.  
  66.         auto char text [MAXEXT];
  67.  
  68.         strcpy( file, strtok( ffblk.ff_name, "." ) );
  69.         strcpy( text, strtok( NULL, "." ) );
  70.         strrev( text );
  71.         strcat( text, "." );
  72.         strcpy( ext, strrev( text ) );
  73.  
  74.     }
  75.     fnmerge( name, drive, dir, file, ext );
  76.     strupr( name );
  77.     
  78.     return( name );
  79. }